<?php
!defined ('P_W') && exit('Forbidden');

class PW_HouseFieldsDB extends BaseDB {

	var $_tableName = "pw_house_fields";
	
	function getByIds($type, $ids) {
		$query = $this->_db->query('SELECT * FROM ' . $this->_tableName . ' WHERE type=' . pwEscape($type) . ' AND id IN(' . pwImplode($ids) . ')');
		return $this->_getAllResultFromQuery($query);
	}

	function getById($type, $id) {
		return $this->_db->get_one('SELECT * FROM ' . $this->_tableName . ' WHERE type=' . pwEscape($type) . ' AND id=' . pwEscape($id));
	}

	function add($data) {
		$this->_db->update('INSERT INTO ' . $this->_tableName . ' SET ' . pwSqlSingle($data));
	}

	function deleteByIds($type, $ids) {
		$this->_db->update('DELETE FROM ' . $this->_tableName . ' WHERE type=' . pwEscape($type) . ' AND id IN (' . pwImplode($ids) . ')');
	}

	function updateByIds($type, $ids, $data) {
		$this->_db->update('UPDATE ' . $this->_tableName . ' SET ' . pwSqlSingle($data) . ' WHERE type=' . pwEscape($type) . ' AND id IN (' . pwImplode($ids) . ')');
	}

	function updateById($type, $id, $data) {
		$this->_db->update('UPDATE ' . $this->_tableName . ' SET ' . pwSqlSingle($data) . ' WHERE type=' . pwEscape($type) . ' AND id=' . pwEscape($id));
	}

	function getByValue($type, $value, $except) {
		$_sql_add = '';
		$except && $_sql_add = ' AND id!=' . pwEscape($except);
		return $this->_db->get_one('SELECT * FROM ' . $this->_tableName . ' WHERE type=' . pwEscape($type) . ' AND fieldvalue=' . pwEscape($value) . $_sql_add);
	}

	function getMinId($type) {
		if (!$this->_db->get_one('SELECT id FROM ' . $this->_tableName . ' WHERE type=' . pwEscape($type) . " AND id='1'")) {
			return 1;
		}
		return $this->_db->get_value('SELECT (a.id+1) AS MinId FROM ' . $this->_tableName . ' a LEFT JOIN ' . $this->_tableName . ' b ON a.type=b.type AND a.id=b.id-1 WHERE a.type=' . pwEscape($type) . ' AND b.id IS NULL LIMIT 1');
	}
	
	function getTypeList($type,$page,$perpage){
		if (!S::isNum($page) || !S::isNum($perpage) || empty($type)){
			return array();
		} 
		$offset = ($page - 1) * $perpage;
		$sql = 'SELECT * FROM '.$this->_tableName.'  WHERE type=' . pwEscape($type) . ' ORDER BY vieworder,id '.$this->_Limit($offset,$perpage);
		$query = $this->_db->query($sql);
		return  $this->_getAllResultFromQuery($query);
	}
	
	function getAllTypeList($type,$ifopen = 1){
		if(empty($type))return array();
		if($ifopen) $_where = ' AND ifopen = 1 ';
		$sql = 'SELECT * FROM '.$this->_tableName.'  WHERE type=' . pwEscape($type) . $_where .' ORDER BY vieworder,id ';
		$query = $this->_db->query($sql);
		return  $this->_getAllResultFromQuery($query);
		
	}
	
	function getMaxVieworder($type) {
		$_sql = 'SELECT Max(vieworder) FROM ' . $this->_tableName . ' WHERE type=' . pwEscape($type);
		return $this->_db->get_value($_sql);
	}
	
	function getCountTypeList($type){
		$sql = 'SELECT count(*) FROM '.$this->_tableName.'  WHERE type=' . pwEscape($type);
		return $this->_db->get_value($sql);
		
	}
	
	function getOpenHouseFieldList(){
		$sql = 'SELECT * FROM '.$this->_tableName.' WHERE ifopen=1 ORDER BY vieworder,id';
		$query = $this->_db->query($sql);
		return  $this->_getAllResultFromQuery($query);
	}
}
?>